<?php
namespace App\Services;

use App\Models\Coupon;
use App\Models\User;
use App\Models\UserCoupon;

class UserCouponService
{
    protected $time = null;

    public function __construct()
    {
        $this->time = time();
    }

    //领取红包
    public function getCoupon($user_id, $coupon_type)
    {
        $user = User::find($user_id);
        if(empty($user))
        {
            return false;
        }

        $coupon = Coupon::query()->where('type',$coupon_type)->first();
        if(empty($coupon))
        {
            return false;
        }

        $user_coupon = new UserCoupon([
            'coupon_id'             => $coupon->id,
            'coupon_order_price'    => $coupon->order_price,
            'coupon_days'           => $coupon->days,
            'coupon_type'           => $coupon->type,
            'coupon_amount'         => $coupon->amount,
            'coupon_title'          => $coupon->title,
            'created_at'            => $this->time,
            'state'                 => 0,
            'expired_at'            => $this->time + 86400*$coupon->days,
        ]);
        $user_coupon->user()->associate($user);
        return $user_coupon->save();
    }

    //获取买书红包列表
    public function getUserCouponList($user_id, $type, $page = 1, $page_size = 10)
    {
        $this->_handle_expired_coupons($user_id);
        $builder = UserCoupon::query()->where('user_id',$user_id);

        switch ($type)
        {
            case 1:
                //未使用
                $builder->where('state','=',0);
                break;
            case 2:
                //已使用
                $builder->where('state','=',1);
                break;
            case 3:
                //已过期
                $builder->where('state','=',2);
                break;
        }
        $builder->orderBy('id','desc');
        $columns = ['id','coupon_id','coupon_order_price','coupon_title','coupon_amount','coupon_type','expired_at','state'];
        $list = $builder->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);

        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'                => $item->id,
                    'coupon_id'         => $item->coupon_id,
                    'coupon_order_price'=> $item->coupon_order_price,
                    'coupon_title'      => $item->coupon_title,
                    'coupon_amount'     => $item->coupon_amount,
                    'coupon_type'       => $item->coupon_type,
                    'expired_at'        => $item->expired_at,
                    'state'             => $item->state,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];
        return $data;
    }

    public function getOrderCoupons($user_id, $book_total_price)
    {
        $this->_handle_expired_coupons($user_id);
        $list = UserCoupon::query()->where('user_id',$user_id)->where('state',0)->get();

        $items = [];
        $can_use_num = 0;
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $can_use = 0;
                if($book_total_price >= $v->coupon_order_price)
                {
                    $can_use = 1;
                    $can_use_num++;
                }
                $items[] = [
                    'id'                => $v->id,
                    'coupon_id'         => $v->coupon_id,
                    'coupon_order_price'=> $v->coupon_order_price,
                    'coupon_title'      => $v->coupon_title,
                    'coupon_amount'     => $v->coupon_amount,
                    'coupon_type'       => $v->coupon_type,
                    'expired_at'        => $v->expired_at,
                    'can_use'          => $can_use,
                ];
            }
        }

        $result = [
            'can_user_num'  => $can_use_num,
            'items'         => $items,
        ];
        return $result;
    }

    //处理过期的红包
    private function _handle_expired_coupons($user_id)
    {
        $list = UserCoupon::query()->where('user_id',$user_id)->where('expired_at','<=',$this->time)->where('state',0)->get();
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $v->update(['state'=>2]);
            }
        }
    }

}
